﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using EPiServer.Licensing;
using EPiServer.Framework.Initialization;
using Auros.EPiSiteMap.Data;

namespace Auros.EPiSiteMap.WebControls
{
    public class SiteMapTableRow : TableRow
    {
        protected TableCell SitemapEnabledCell;
        protected TableCell HostCell;
        protected List<TableCell> LanguageCells;
        protected TableCell FileNameCell;
        protected TableCell LastCreatedCell;

        public CheckBox SitemapEnabledCheckbox;
        public List<CheckBoxValue> LanguageCheckBoxes;
        public TextBox FileNameTextBox;

        public string SiteId { get; set; }
        public HostSetting HostSettings { get; set; }

        public SiteMapHostConfig SitemapHostData { get; set; }

        /// <summary>
        ///   Creates a new sitemap row in the table.
        /// </summary>
        public SiteMapTableRow(String siteId, HostSetting data)
        {
            this.SiteId = siteId;
            this.HostSettings = data;
        }

        /// <summary>
        ///   Creates a new sitemap row in the table.
        /// </summary>
        public SiteMapTableRow(String siteId, HostSetting data, SiteMapHostConfig sitemapHostConfigData)
        {
            this.SiteId = siteId;
            this.HostSettings = data;
            this.SitemapHostData = sitemapHostConfigData;
        }

        protected override void OnInit(EventArgs e)
        {
            this.SitemapEnabledCell = new TableCell();  
            this.HostCell = new TableCell();
            this.LanguageCells = new List<TableCell>();
            this.FileNameCell = new TableCell();

            this.SitemapEnabledCheckbox = new CheckBox();
            this.LanguageCheckBoxes = new List<CheckBoxValue>();
            this.FileNameTextBox = new TextBox();

            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            this.SitemapEnabledCell.Controls.Add(this.SitemapEnabledCheckbox);

            this.HostCell.Text = this.HostSettings.SiteHost;

            // Create language cells
            foreach (var language in EPiServer.DataAbstraction.LanguageBranch.ListEnabled())
            {
                // Create the checkbox
                CheckBoxValue checkbox = new CheckBoxValue() { Value = language.LanguageID };
                this.LanguageCheckBoxes.Add(checkbox);

                // If the language is the default for the host, it is automatically checked and disabled.
                if (this.HostSettings.Language == language.LanguageID)
                {
                    checkbox.Checked = true;
                    checkbox.Enabled = false;
                    checkbox.ToolTip = string.Format(
                        "Language '{0}' is the default culture for this host and will be automatically included.", language.LanguageID);
                }

                // If the language is mapped to another host for this site, it is unchecked and disabled.
                var hostMappingForLangauge = SiteMappingConfiguration.Instance.HostToLanguageMapForSite(this.SiteId)
                    .Where(kvp => kvp.Value != this.HostSettings.Language)
                    .FirstOrDefault(kvp => kvp.Value == language.LanguageID);

                if (hostMappingForLangauge.Key != null)
                {
                    checkbox.Checked = false;
                    checkbox.Enabled = false;
                    checkbox.ToolTip = string.Format(
                        "Language '{0}' is used as the default culture for host {1}. Links for pages in this language will be included in the sitemap for that host.",
                        language.LanguageID, hostMappingForLangauge.Key);
                }

                // Create the containing cell
                TableCell checkboxCell = new TableCell();
                this.LanguageCells.Add(checkboxCell);

                checkboxCell.Controls.Add(checkbox);
            }

            this.FileNameCell.Controls.Add(this.FileNameTextBox);
            
            // Add to row
            this.Cells.Add(this.SitemapEnabledCell);
            this.Cells.Add(this.HostCell);
            this.Cells.AddRange(this.LanguageCells.ToArray());
            this.Cells.Add(this.FileNameCell);

            if (this.HostSettings.SiteHost == "*")
            {
                this.SitemapEnabledCheckbox.Enabled = false;

                this.SitemapEnabledCheckbox.ToolTip = "Cannot create sitemaps for wildcard hosts.";

                foreach (var checkbox in this.LanguageCheckBoxes)
                {
                    checkbox.Enabled = false;
                }

                this.FileNameTextBox.Enabled = false;
            }

            if (!Page.IsPostBack && this.SitemapHostData != null)
            {
                this.SitemapEnabledCheckbox.Checked = this.SitemapHostData.Enabled;

                // Data bind from DDS
                this.FileNameTextBox.Text = this.SitemapHostData.FileName;

                foreach (var langauge in this.SitemapHostData.Langauges)
                {
                    var checkbox = this.LanguageCheckBoxes.FirstOrDefault(item => item.Value == langauge && item.Enabled == true);

                    if (checkbox != null)
                    {
                        checkbox.Checked = true;
                    }
                }
            }

            base.OnLoad(e);
        }
    }

    /// <summary>
    ///   Checkbox supporting a value property.
    /// </summary>
    public class CheckBoxValue : CheckBox
    {
        public string Value
        {
            get;
            set;
        }
    }
}